package com.haiyou.dc.aggregate.list;

public class MyLinkList01 {

    private Node first;
    private Node last;

    public void add(Object element) {
        Node node = new Node(element);
        if (first == null) {
            first = node;
            last = node;
        } else {
            node.setPrevious(last);
            node.setNext(null);

            last.setNext(node);
            last = node;
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Node temp = first;
        while (temp != null) {
            if (sb.length() > 0) {
                sb.append(",");
            }
            sb.append(temp.getElement());
            temp = temp.getNext();
        }
        return sb.insert(0, "[").append("]").toString();
    }

    public static void main(String[] args) {
        MyLinkList01 myLinkList01 = new MyLinkList01();
        System.out.println(myLinkList01);
        myLinkList01.add("a");
        myLinkList01.add("b");
        myLinkList01.add("c");
        myLinkList01.add("d");
        myLinkList01.add("c");
        myLinkList01.add("d");
        System.out.println(myLinkList01);
    }
}
